package com.yiren.dbaa.module.practice_set.basestation.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.util.BaseSignalUtil;
import com.yiren.dbaa.view.toast.MessageBox;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Describe 基站选址验证页面
 *
 * @author WRX
 * @date 2022/12/14
 */
public class LocationVerifyActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_satellite)
    TextView tvSatellite;
    @BindView(R.id.tv_longitude)
    TextView tvLongitude;
    @BindView(R.id.tv_net_speed)
    TextView tvNetSpeed;
    @BindView(R.id.tv_latitude)
    TextView tvLatitude;
    @BindView(R.id.tv_net_strong)
    TextView tvNetStrong;
    @BindView(R.id.tv_altitude)
    TextView tvAltitude;
    @BindView(R.id.tv_base_location)
    TextView tvBaseLocation;

    /*** 需要达到的卫星数 */
    private final int reqSatelliteNum = 0;
    /*** 需要验证的次数 */
    private final int reqAuthCount = 3;

    private BaseSignalUtil baseSignalUtil;
    /*** 实时卫星数 */
    private int satelliteNum = 0;
    /*** 卫星数少于指定卫星数验证次数 */
    private int minAuthCount = 0;
    /*** 卫星数大于等于指定卫星数验证次数 */
    private int maxAuthCount = 0;
    /*** 是否开始验证 */
    private boolean isStartAuth = true;
    /*** 当前的验证状态 0：验证中 1：验证失败 2：验证通过 */
    private int status = 0;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    tvNetSpeed.setText(msg.obj.toString());
                    break;
                case 200:
                    tvNetStrong.setText(msg.obj.toString() + "dBm");
                    break;
                case 300:
                    String[] locations = msg.obj.toString().split(":");
                    tvLongitude.setText(locations[1]);
                    tvLatitude.setText(locations[2]);
                    tvAltitude.setText(StringUtil.format(Double.parseDouble(locations[3]), StringUtil.GPS_ALTITUDE_FORMAT));
                    satelliteNum = StringUtil.isNothing(locations[0]) ? 0 : Double.valueOf(locations[0]).intValue();
                    tvSatellite.setText(satelliteNum + "");
                    if (isStartAuth) {
                        if (satelliteNum < reqSatelliteNum) {
                            minAuthCount++;
                            if (minAuthCount > reqAuthCount) {
                                status = 1;
                                isStartAuth = false;
                                tvBaseLocation.setText("验证未通过，重新验证");
                            }
                        } else {
                            maxAuthCount++;
                            if (maxAuthCount > reqAuthCount) {
                                status = 2;
                                isStartAuth = false;
                                tvBaseLocation.setText("验证通过，下一步标定基站");
                            }
                        }
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_verify);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        tvTitle.setText("基站选址验证");
        baseSignalUtil = new BaseSignalUtil(this, mHandler);
        baseSignalUtil.startTask();
    }

    @OnClick({R.id.iv_back, R.id.tv_base_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.tv_base_location:
                if (status == 0) {    //验证中
                    MessageBox.show(this, "正在验证中，请稍后...");
                } else if (status == 1) {  //验证失败
                    minAuthCount = 0;
                    maxAuthCount = 0;
                    status = 0;
                    isStartAuth = true;
                    tvBaseLocation.setText("正在验证基站选址，请稍后......");
                } else {   //验证成功
                    baseSignalUtil.cancelTask();
                    startActivity(new Intent(this, CalibrationActivity.class));
                    finish();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        baseSignalUtil.cancelTask();
    }
}
